// 正式环境配置
const PAGE_URL = "https://www.51bmj.cn";
const BASE_URL = "https://bootapi.51bmj.cn";
// const BASE_URL = "http://localhost:9999"; // 本地调试用
const APP_ID = "wxda141ae36aedc306";

// 测试环境配置
// const APP_ID = "wx0efacf82e40d21b5";
// const PAGE_URL = 'http://web.51bmj.net'
// const BASE_URL = 'https://bootapi.51bmj.net'

// const APP_ID = "wx0efacf82e40d21b5";
// const PAGE_URL = 'http://192.168.2.131:7002'
// const BASE_URL = 'http://192.168.2.131:7002'

// const APP_ID = "wx0efacf82e40d21b5";
// const PAGE_URL = 'http://192.168.3.108:7002'
// const BASE_URL = 'http://192.168.3.108:7002'

// const IMG_BASE_URL = BASE_URL + '/sys/common/static/'
const FILE_BASE_URL = BASE_URL + "/sys/common/static/";
const IMG_BASE_URL = "https://dm-bmj.oss-cn-guangzhou.aliyuncs.com/";

// post请求封装
function postRequest(url, data) {
  var promise = new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + url,
      data,
      method: "POST",
      header: {
        realServerName: "www.51bmj.net",
        "X-Access-Token": uni.getStorageSync("Mtoken"),
      },
      success: function (res) {
        //返回什么就相应的做调整
        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          // 请求服务器成功，但是由于服务器没有数据返回，此时无code。会导致这个空数据
          //接口后面的then执行
          // 不下去，导致报错，所以还是要resolve下，这样起码有个返回值，
          //不会被阻断在那里执行不下去！
          resolve(res.data.msg);
        }
      },
      error: function (e) {
        reject("网络出错");
      },
    });
  });
  return promise;
}

// get
function getRequest(url, data) {
  var promise = new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + url,
      data,
      method: "GET",
      dataType: "json",
      header: {
        realServerName: "www.51bmj.net",
        // "content-type": "application/json;charset=UTF-8",
        "X-Access-Token": uni.getStorageSync("Mtoken"),
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code === 10001) {
            uni.removeStorageSync("Mtoken");
          }
          resolve(res.data);
        } else {
          resolve(res.data);
        }
      },
      error: function (e) {
        reject("网络出错");
      },
    });
  });
  return promise;
}

// delete
function deleteRequest(url, data) {
  var promise = new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + url,
      data,
      method: "DELETE",
      dataType: "json",
      header: {
        realServerName: "www.51bmj.net",
        // "content-type": "application/json;charset=UTF-8",
        "X-Access-Token": uni.getStorageSync("Mtoken"),
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code === 10001) {
            uni.removeStorageSync("Mtoken");
          }
          resolve(res.data);
        } else {
          resolve(res.data);
        }
      },
      error: function (e) {
        reject("网络出错");
      },
    });
  });
  return promise;
}

module.exports = {
  post: postRequest,
  get: getRequest,
  deleteFun: deleteRequest,
  imgUrl: IMG_BASE_URL,
  fileUrl: FILE_BASE_URL,
  pageUrl: PAGE_URL,
  appId: APP_ID,
};
